package com.cjzuji.rental.dao.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjzuji.rental.dao.OrderContractDao;
import com.cjzuji.rental.mapper.OrderContractMapper;
import com.cjzuji.rental.pojo.OrderContract;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author bag
 * @version V1.0
 * @date 2025/7/7 21:19
 * @since 1.0
 */

@Service
public class OrderContractDaoImpl implements OrderContractDao {
    private static final Logger logger = LoggerFactory.getLogger(OrderContractDaoImpl.class);
    @Autowired
    OrderContractMapper orderContractMapper;

    @Override
    @Transactional
    public  void save(List<Map<String,Object>> data, Long orderID){
        List<OrderContract>  contractList = new ArrayList<>();
        for (Map<String, Object> item : data){
            OrderContract orderContract = OrderContract.builder()
                    .orderId(orderID)
                    .contractNo(item.get("agreementCode").toString())
                    .title(item.get("title").toString())
                    .url(item.get("url").toString())
                    .addTime(item.get("createTime").toString())
                    .createTime(new Date())
                    .updateTime(new Date())
                    .build();
            contractList.add(orderContract);
        }

        try {
            orderContractMapper.insertBatchSomeColumn(contractList);
        }catch (Exception e){
            logger.info("订单合同添加错误" + e.getMessage());
            throw e; // 重新抛出，确保事务回滚
        }
    }
}
